#include "bsp_uart.h"

//���ڹ滮
// USART6 - SBUS (�̶�)
// USART2 - ��λ�����գ���ʱ��
// USART1 - TOFģ�� ����ʱ��

extern UART_HandleTypeDef huart6,huart2,huart3,huart1,huart5;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if( huart->Instance == USART6 )
	{
		SBUS_RecvCallback(); 
	}
	else if( huart->Instance == USART3 )
	{
//		ano_sensor_recv_parse();
		MTF02_USART_RecvCallback();
//		Tof_USART_RecvCallback();
	}
}


void SBUS_StartRecv(uint8_t *data)
{
	HAL_UART_Receive_IT(&huart6,data,1);  
}

#include "data_transmit.h"
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	
	if( huart->Instance == USART2 )
	{
		
	}
}

void _sys_exit(int x) 
{ 
	x = x; 
} 
int fputc(int ch, FILE *f)    
{      
//  while((USART2->SR&0X40)==0);
//    USART2->DR = (uint8_t) ch;      
	return ch;
}

uint8_t digital_twin_send_data(uint8_t *buf,uint16_t length)
{
	if( HAL_UART_Transmit_DMA(&huart2,buf,length) != HAL_OK )
	{
		return 1;
	}
	return 0;
}

void digital_twin_start_recv(uint8_t *buf,uint16_t length)
{
	HAL_UART_Receive_IT(&huart2,buf,length);
}

uint8_t HC_SendData(uint8_t *buf,uint16_t length)
{
	if( HAL_UART_Transmit_DMA(&huart2,buf,length) != HAL_OK )
	{
		return 1;
	}
	return 0;
}

void HC_StartRecv(uint8_t *data)
{
	HAL_UART_Receive_IT(&huart2,data,1);  
}



void MTF02_StartRecv(uint8_t *buf,uint16_t length)
{
//	HAL_UART_Receive_IT(&huart3,buf,length);
	HAL_UART_Receive_IT(&huart5,buf,length);
}

void Tof_StartRecv(uint8_t *buf,uint16_t length)
{ 
	HAL_UART_Receive_IT(&huart3,buf,length);
}

void ano_sensor_start_recv(uint8_t *data)
{
	HAL_UART_Receive_IT(&huart3,data,1); 
}


void USART2_IdleCallback(void)
{
	__HAL_UART_CLEAR_IDLEFLAG(&huart2);
	uint16_t length = 0;

	length = huart2.RxXferSize - huart2.RxXferCount;
	if( huart2.RxXferCount == 0 )
		length = 0;
	
	if( length )	//chip1
	{
		digital_twin_recv_parse(length);		
	}

	HAL_UART_Abort(&huart2);	

	digital_twin_init();
}


void USART3_IdleCallback(void)
{
	__HAL_UART_CLEAR_IDLEFLAG(&huart3);
	uint16_t length = 0;

	length = huart3.RxXferSize - huart3.RxXferCount;
	if( huart3.RxXferCount == 0 )
		length = 0;
	
	if( length )	//chip1
	{
//		MTF02_USART_RecvCallback();
//		Tof_USART_RecvCallback();
	}

	HAL_UART_Abort(&huart3);	

//	Tof_Init();
	MTF02_Init();
}


// zzt adding here:
void UART5_IdleCallback(void)
{
	__HAL_UART_CLEAR_IDLEFLAG(&huart5);
	uint16_t length = 0;

	length = huart5.RxXferSize - huart5.RxXferCount;
	if( huart5.RxXferCount == 0 )
		length = 0;
	
	if( length )
	{
		MTF02_USART_RecvCallback();
		//used by ano sensor
	}

	HAL_UART_Abort(&huart5);	

	MTF02_Init();
}
